package com.jdl.iscm.common;

import com.google.common.collect.Lists;
import com.jdl.iscm.entity.enums.StockDimensionEnum;
import com.jdl.iscm.entity.proterties.ReplenishXpathProperties;
import org.openqa.selenium.WebDriver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: Ran
 * @Date: 2021/8/24
 **/
@Component
public class DimensionCmpt {

    @Autowired
    private CommonCmpt commonCmpt;

    public void setDimValueByDimKey(WebDriver webDriver, ReplenishXpathProperties xpathProperties, Map<String, String> dimensionMap) {
        dimensionMap.keySet().forEach(key -> {
            String v = dimensionMap.get(key);
            List<String> targetS = Lists.newArrayList(JoinUtil.textJoin(xpathProperties, v));
            StockDimensionEnum dimensionEnum = StockDimensionEnum.ofCode(key);
            switch (dimensionEnum) {
                case SKU:
                    commonCmpt.click(webDriver, xpathProperties.getConfigDimSku());
                    commonCmpt.drpDwnSeachClick(webDriver, xpathProperties.getConfigDimSkuInput(), dimensionMap.get(key), targetS);
                    break;
                case WH:
                    commonCmpt.click(webDriver, xpathProperties.getConfigDimWh());
                    commonCmpt.drpDwnSeachClick(webDriver, xpathProperties.getConfigDimWhInput(), dimensionMap.get(key), targetS);
                    break;
                case BAND:
                    commonCmpt.click(webDriver, xpathProperties.getConfigDimBand());
                    commonCmpt.drpDwnClick(webDriver, xpathProperties.getConfigDimBandInput(), targetS);
                    break;
                case KIND:
                    commonCmpt.click(webDriver, xpathProperties.getConfigDimKind());
                    //品类涉及级联需要特殊处理  入参为 & 拼接
                    String[] kinds = v.split("&");
                    List<String> list = Arrays.stream(kinds).map(kind -> JoinUtil.textJoin(xpathProperties, kind)).collect(Collectors.toList());
                    commonCmpt.drpDwnClick(webDriver, xpathProperties.getConfigDimKindInput(), list);
                    break;
                case Global:
                    commonCmpt.click(webDriver, xpathProperties.getConfigDimGlobal());
                    break;
            }
        });
    }
}
